package com.atguigu.eduorder.controller;


import com.atguigu.eduorder.service.PayLogService;
import com.atguigu.oss.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author luomingliang
 * @since 2020-07-13
 */
@CrossOrigin
@RestController
@RequestMapping("/eduorder/paylog")
public class PayLogController {
    @Autowired
    private PayLogService payLogService;

    //生成微信支付二维码接口
    @GetMapping("createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo) {
        //返回信息, 包含二维码地址, 还有其他信息
        Map<String,Object> map = payLogService.createNative(orderNo);
        System.out.println("===================================================");
        System.out.println("返回二维码集合:"+map);
        return R.ok().data(map);
    }

    //根据订单号查询订单支付状态
    @GetMapping("queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo) {
        System.out.println("queryPayStatus:"+orderNo);
        Map<String,String> map = payLogService.queryPayStatus(orderNo);
        System.out.println("===================================================");
        System.out.println("查询订单状态map集合:"+map);
        if (map == null) {
            return R.error().message("支付出错了");
        }
        //如果map不为空, 通过map获取订单状态
        if (map.get("trade_state").equals("SUCCESS")) {
            //向支付表中添加记录, 并更新订单表支付状态
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }
        return R.ok().code(25000).message("支付中...");
    }

}

